function getUrlParam(url, name) {
    // 获取参数
    var url = url.split('?')[1];
    // 正则筛选地址栏
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    // 匹配目标参数
    var result = url.substr(1).match(reg);
    //返回参数值
    return result ? decodeURIComponent(result[2]) : null;
}

function initFixedList() {
    var listStr = `
        <div class="lbj-wrap">
            <div class="list"></div>
            <div class="toggle-btn">></div>
        </div>
    `
    $('body').append(listStr);
    $('.toggle-btn').on('click', e => {
        if ($('.lbj-wrap').hasClass('hide')) {
            $('.lbj-wrap').removeClass('hide')
            $('.toggle-btn').html('>')
        } else {
            $('.lbj-wrap').addClass('hide')
            $('.toggle-btn').html('<')
        }
    })
}
function pushLinkItem(url) {
    const name = getUrlParam(url, 'keywords') || getUrlParam(url, 'wd')
    const item = `
        <a class="item" href="${url}" target="_blank">
            <div class="name">${name}</div>
            <div class="link">${url}</div>
        </a>
    `
    $('.lbj-wrap .list').append(item)
}
function pushItem(item) {
    const textarea = document.createElement('textarea')
    textarea.value = item
    textarea.style.height = '20em'
    $('body').append(textarea)
    const button = document.createElement('button')
    button.innerHTML = '复制'
    button.addEventListener('click', e => {
        textarea.select()
        document.execCommand("Copy")
    })
    $('body').append(button)
}

function getJson() {
    const json = $('body pre').html()
    const data = JSON.parse(json)
    console.log(data)
    $('body').empty()
    return data
}

function formatAMapBody() {
    const data = getJson().data
    const title = data.poi_list[0].disp_name
    $('body').append(`<h1>${title}</h1>`)
    $('head').append(`<title>${title}</title>`)
    let arr = []
    const poi = data.poi_list[0]
    const it = poi.domain_list.filter(p => p.value && p.type === 'text')
    for (let road of it) {
        arr.push(road.value)
    }
    pushItem(arr.join('|'))
}
function formatBMapBody() {
    const data = getJson()
    const item = data.content[0]
    const title = item.addr + item.name
    $('body').append(`<h1>${title}</h1>`)
    $('head').append(`<title>${title}</title>`)
    pushItem(item.profile_geo)
}
$(() => {
    const wHref = window.location.href
    if (
        !wHref.includes('.amap.com') &&
        !wHref.includes('map.baidu.com')
    ) {
        return
    }
    if (wHref.includes('.amap.com/service/poiInfo')) {
        formatAMapBody()
        return
    }
    if (wHref.includes('map.baidu.com/?newmap=1&reqflag=pcmap')) {
        formatBMapBody()
        return
    }
    initFixedList()
    chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
        console.log(request)
        if (request.cmd == 'url') {
            const { url } = request.value
            pushItem(url)
            pushLinkItem(url)
            window.open(url)
        }
    })
})
